'''
 ******************************************************************************
 *  Copyright 2010 Vijay Pandurangan
 *  Parts copyright Jumo International.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 ******************************************************************************

Tests for server and exported variables

@author: vijayp
'''
import unittest
import logging
from ting3.common.t3httpserver import *
from ting3.common.t3expvar import *

def setup_logging():
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO)
    formatter = logging.Formatter(
      "%(levelname).1s%(asctime)s %(filename)s:%(lineno)d] %(message)s",
      datefmt='%Y%m%d:%H%M%S')
    ch.setFormatter(formatter)
  
    for LOG in [logging.getLogger(''), logging.getLogger()]:
      LOG.setLevel(logging.INFO)
      LOG.addHandler(ch)

class TestClass(object):
  def __init__(self):
    self.notexp = 1
    self.hello_exp = 1
    
  def foo_exp(self):
    pass
  
class Base:
  pass
class A:
  class AA(Base):
    pass
  class AC(Base):
    pass
class B(A):
  class AAB(A.AA):
    pass
  class AAA(Base):
    pass
  class AC(Base):
    pass  
  
  
class DescendantTest(unittest.TestCase):
  def testDesc(self):
    
    assert GetDescendants(B(), Base) == [A.AA, B.AAA, B.AAB, B.AC], GetDescendants(B(), Base)
    assert GetDescendants(B(), Base) != [A.AA, B.AAA, B.AAB, A.AC]

class RateTrackerTest(unittest.TestCase):
  class TestClass:
    s = 1

  def setUp(self):
    setup_logging()

  def testRateTracker(self):
    rt = RateTracker(RateTrackerTest.TestClass(), 's')

    try:
      rt = RateTracker(RateTrackerTest.TestClass(), 's',
                              quanta=[10,11])
      
      assert 0
    except BadQuantaError:
      pass
    
    for _ in range(15):
      self.TestClass.s += 1
      rt.MinQuantumExpired()
      
    assert rt.AsList() == [('TestClass.s', '16'),
                           ('TestClass.s-00001', '0'), 
                           ('TestClass.s-00010', '9'), 
                           ('TestClass.s-00100', '15')
                           ], rt.AsList()
    
    self.TestClass.s += 1
    

    assert (rt.AsList() == [
                           ('TestClass.s', '17'), 
                           ('TestClass.s-00001', '1'), 
                           ('TestClass.s-00010', '10'), 
                           ('TestClass.s-00100', '16')]), rt.AsList()

    
  def RateTrackerInIOLoop(self):
    #TODO(vijayp): test this code properly
    pass
    
class ExpVarTest(unittest.TestCase):

  def setUp(self):
    self._exp = ExpDH.instance()
    pass
  
  def tearDown(self):
    pass

  def testIntrospection(self):
    c = TestClass()
    self._exp.AutoAddClass(c, 'class')
    z = dict(self._exp._GetVariableList())
    print z
    assert z.has_key('class.hello_exp') 
    assert (z['class.hello_exp'] == '1') 
    assert not z.has_key('class.foo_exp')
    
    c.hello_exp += 1
    z = dict(self._exp._GetVariableList())
    assert z.has_key('class.hello_exp')
    assert z['class.hello_exp'] == '2' 
    
    self.mycrap = 1
    self._exp.WatchVariable(self, 'mycrap')
    z = dict(self._exp._GetVariableList())
    print z  
    assert z.has_key('ExpVarTest.mycrap')
    assert z['ExpVarTest.mycrap'] == '1', z
    self.mycrap = 'asdf'
    z = dict(self._exp._GetVariableList())  
    print z  
    assert z.has_key('ExpVarTest.mycrap')
    assert z['ExpVarTest.mycrap'] == 'asdf', z['ExpVarTest.mycrap']
    
    pass





if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
    
    